package com.imageim.yimirror.common.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.imageim.yimirror.R;

/**
 * Created by Bruce.Zhou on 2018/4/9.
 */

public class MemeoryImgaeView extends View {

    private Bitmap mBitmap;
    private Paint mPaint;
    private int mWidth;
    private int mHeight;

    public MemeoryImgaeView(Context context) {
        super(context);
        init();
    }

    public MemeoryImgaeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MemeoryImgaeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setFilterBitmap(true);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(10);
        // 设置绘制时各图形的结合方式，如平滑效果等
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setMaskFilter(new BlurMaskFilter(50, BlurMaskFilter.Blur.SOLID));
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.landing1);
    }

    /**
     * Drawable转Bitmap
     */
    private Bitmap getBitmapFromDrawable(Drawable drawable) {
//        super.setScaleType(ScaleType.CENTER_CROP);
        if (drawable == null) {
            return null;
        }
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
        try {
            Bitmap bitmap;
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        } catch (OutOfMemoryError e) {
            return null;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        //按照逆时针方向添加一个圆
        path.addCircle(mWidth / 2, mHeight / 2, mWidth / 2, Path.Direction.CCW);
        //先将canvas保存
        canvas.save();
        //把canvas修剪成指定的路径区域
        canvas.clipPath(path);
        //绘制图形Bitmap

//        canvas.drawBitmap(mBitmap, 0, 0, mPaint);

        canvas.drawCircle(mWidth / 2, mHeight / 2,100,mPaint);

        //恢复Canvas
        canvas.restore();
    }
}
